package pers.mihao.quickstream.common.model.convertor;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.mapstruct.AfterMapping;
import org.mapstruct.MappingTarget;
import pers.mihao.quickstream.common.model.BaseDO;
import pers.mihao.quickstream.common.model.Features;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author mihao
 * @since 2024/6/9
 */
public interface BaseConvert {


    default Features map(String features) {
        return new Features(Optional.ofNullable(features).map(s -> JSONObject.parseObject(s, new TypeReference<Map<String, String>>() {
                }))
                .orElse(new HashMap<>()));
    }

    default String map(Features features) {
        return JSONObject.toJSONString(
                Optional.ofNullable(features).orElse(new Features()).getFeaturesMap());
    }

    @AfterMapping
    default void afterMapping2DO(BaseDO source, @MappingTarget Object target) {
        if (source == null) {
            return;
        }

    }

    @AfterMapping
    default void afterDOMapping2DTO(Object source, @MappingTarget BaseDO target) {
        if (source == null) {
            return;
        }

    }
}

